home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / RM_ALL.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  6KB  |  195 lines

  1. /* +++Date last modified: 28-Sep-1996 */
  2.  
  3. /*
  4. **  Remove all files and (optionally) subdirectories
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13. #include <dos.h>
  14. #include <ctype.h>
  15. #include "sniptype.h"
  16. #include "dirport.h"
  17. #include "unistd.h"
  18.  
  19. #define MAX_PATH 80
  20.  
  21. #if (defined(_MSC_VER) && (_MSC_VER >= 700)) || (defined(__SC__))
  22.  /* Make FP_xxx macros lvalues as in older versions */
  23.  #undef FP_SEG
  24.  #undef FP_OFF
  25.  #define FP_SEG(fp)    ((unsigned)((unsigned long)(fp) >> 16))
  26.  #define FP_OFF(fp)    ((unsigned)(fp && 0xffff))
  27. #endif
  28.  
  29. /* Select one of the following - remove() is ANSI       */
  30.  
  31. #define rmfunc remove
  32. /* #define rmfunc unlink */
  33.  
  34. #define show(s) fputs((s), stderr)
  35.  
  36. Boolean_T recurse = False_, gobble = False_, ignore = False_;
  37.  
  38. char *mask = "*.*";
  39.  
  40. /*
  41. **  Clean all files from a directory
  42. */
  43.  
  44. void clean_dir(char *path)
  45. {
  46.       char rmpath[MAX_PATH], *rmfile;
  47.       DOSFileData fbuf;
  48.       unsigned attrib = (ignore) ? _A_ANY : 0;
  49.  
  50.       strcpy(rmpath, path);
  51.       if ('\\' != LAST_CHAR(rmpath))
  52.             strcat(rmpath, "\\");
  53.       rmfile = &rmpath[strlen(rmpath)];
  54.       strcpy(rmfile, mask);
  55.       if (0 == FIND_FIRST(rmpath, attrib, &fbuf)) do
  56.       {
  57.             strcpy(rmfile, ff_name(&fbuf));
  58.             if (ignore)
  59.             {
  60.                   union REGS regs;
  61.                   struct SREGS sregs;
  62.  
  63.                   regs.x.ax = 0x4300;
  64.                   regs.x.dx = FP_OFF((char FAR *)rmpath);
  65.                   segread(&sregs);
  66.                   sregs.ds  = FP_SEG((char FAR *)rmpath);
  67.                   intdosx(®s, ®s, &sregs);
  68.                   if (!regs.x.cflag)
  69.                   {
  70.                         regs.x.ax  = 0x4301;
  71.                         regs.x.cx &= ~(_A_RDONLY | _A_HIDDEN | _A_SYSTEM);
  72.                         intdosx(®s, ®s, &sregs);
  73.                         if (regs.x.cflag)
  74.                               printf("unable to delete %s\n", rmpath);
  75.                   }
  76.             }
  77.             rmfunc(rmpath);
  78.             printf("deleting %s\n", rmpath);
  79.       } while (0 == FIND_NEXT(&fbuf));
  80. }
  81.  
  82. /*
  83. **  Process directories
  84. */
  85.  
  86. void do_dir(char *path)
  87. {
  88.       char search[MAX_PATH], new[MAX_PATH];
  89.       DOSFileData ff;
  90.  
  91.       strcpy(search, path);
  92.       if ('\\' != LAST_CHAR(search))
  93.             strcat(search, "\\");
  94.       strcat(search, "*.*");
  95.       if (Success_ == FIND_FIRST(search, _A_ANY, &ff)) do
  96.       {
  97.             if (ff_attr(&ff) & _A_SUBDIR && '.' != *ff_name(&ff))
  98.             {
  99.                   strcpy(new, path);
  100.                   if ('\\' != LAST_CHAR(new))
  101.                         strcat(new, "\\");
  102.                   strcat(new, ff_name(&ff));
  103.                   do_dir(new);
  104.             }
  105.       } while (Success_ == FIND_NEXT(&ff));
  106.       clean_dir(path);
  107.       if (gobble)
  108.             rmdir(path);
  109. }
  110.  
  111. /*
  112. **  Tell 'em they messed up
  113. */
  114.  
  115. void usage(Boolean_T errstat)
  116. {
  117.       if (errstat)
  118.             fputc('\a', stderr);
  119.       show("Usage: RM_ALL directory [...directory] [-eFNAME.EXT] [-rgi?]\n");
  120.       show("switches: -eFNAME.EXT  Remove only files matching mask "
  121.             "(default is \"-e*.*\")\n");
  122.       show("          -r           Recurse subdirectories\n");
  123.       show("          -g           Gobble (delete) empty subdirectories\n");
  124.       show("          -i           Ignore special file attributes "
  125.             "(CAUTION!)\n");
  126.       show("          -?           Display help (this message)\n");
  127.       exit(errstat);
  128. }
  129.  
  130. /*
  131. **  RM_ALL - Deletes all files and (optionally) subdirectories
  132. */
  133.  
  134. int main(int argc, char *argv[])
  135. {
  136.       int i, j;
  137.       Boolean_T found_dir = False_;
  138.       void (*clean_func)(char *) = clean_dir;
  139.  
  140.       for (i = 1; i < argc; ++i)          /* Check for switches         */
  141.       {
  142.             if (NULL == strchr("-/", *argv[i]))
  143.                   continue;               /* Assume it's a filename     */
  144.             for (j = 1; argv[i][j] ; ++j) /* Traverse nested switches   */
  145.             {
  146.                   switch (toupper(argv[i][j]))
  147.                   {
  148.                   case 'R':
  149.                         clean_func = do_dir;
  150.                         break;
  151.  
  152.                   case 'G':
  153.                         gobble = True_;
  154.                         break;
  155.  
  156.                   case 'I':
  157.                         ignore = True_;
  158.                         break;
  159.  
  160.                   case '?':
  161.                         puts("***help***");
  162.                         usage(False_);
  163.                         break;
  164.  
  165.                   case 'E':
  166.                         if (0 == strlen(&argv[i][++j]))
  167.                         {
  168.                               puts("***no file***");
  169.                               usage(Error_);                /* Oops     */
  170.                         }
  171.                         mask = strupr(&argv[i][j]);
  172.                         j += strlen(&argv[i][j]) - 1; /* End of switch  */
  173.                         break;
  174.  
  175.                   default:
  176.                         puts("***default***");
  177.                         usage(Error_);
  178.                   }
  179.             }
  180.       }
  181.       for (i = 1; i < argc; ++i)          /* Scan filenames             */
  182.       {
  183.             if (strchr("/-", *argv[i]))
  184.                   continue;
  185.             found_dir = True_;
  186.             clean_func(argv[i]);
  187.       }
  188.       if (!found_dir)
  189.       {
  190.             puts("***not found***");
  191.             usage(True_);
  192.       }
  193.       return 0;
  194. }
  195.